package com.zhj.rabbitmq.demo.confirm;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhengjibin
 * @version 1.0.0
 * @ClassName ConfirmRabbitmqConfig.java
 * @Description 发布确认功能练习
 * @createTime 2023年03月25日 10:56:00
 */
@Configuration
public class ConfirmRabbitmqConfig {
    public static final String EXCHANGE_NAME = "confirm_exchange";

    public static final String QUEUE_NAME = "confirm_queue";

    public static final String ROUTONG_KEY = "confirm_routing_key";

    public static final String BACKUP_EXCHANGE_NAME = "backup_exchange";
    public static final String BACKUP_QUEUE_NAME = "backup_queue";

    //普通交换机
    @Bean(EXCHANGE_NAME)
    public DirectExchange createExchange(){
        //durable 持久化
        String alternate = "alternate-exchange";//alternate = 替代，表示指明替代交换机
        return ExchangeBuilder.directExchange(EXCHANGE_NAME).durable(true).withArgument(alternate,BACKUP_EXCHANGE_NAME).build();
    }
    //普通队列
    @Bean(QUEUE_NAME)
    public Queue createQueue(){
        Map<String, Object> arguments = new HashMap<>();
        //设置队列优先级参数，可以设置0-255，但是不建议太大，占用内存
        arguments.put("x-max-priority",10);//这设置0-10的优先级参数
        return QueueBuilder.durable(QUEUE_NAME).withArguments(arguments).build();
    }
    //普通交换机与普通队列绑定
    @Bean
    public Binding bindingConfirm(@Qualifier(QUEUE_NAME) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTONG_KEY).noargs();
    }

    ////////////////////////创建备份交换机信息
    @Bean(BACKUP_EXCHANGE_NAME)
    public FanoutExchange createBackupExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }

    @Bean(BACKUP_QUEUE_NAME)
    public Queue createBackupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }
    @Bean
    public Binding bindingBackup(@Qualifier(BACKUP_QUEUE_NAME) Queue queue,@Qualifier(BACKUP_EXCHANGE_NAME) FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }
}
